package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.bean.huojiaBean;
import com.util.DBUtil;

public class huojiaDao {
		
	
	public static boolean add(String A,String B,String C) {//添加商品
		Connection conn=DBUtil.getConnectDb();
		String sql = "insert into huojia (commodity,number,unit_price) VALUES(?,?,?)";
		int rs;
		PreparedStatement stm = null;
		try {
		stm = conn.prepareStatement(sql);
		stm.setString(1,A);
		stm.setString(2,B);
		stm.setString(3,C);
		
		boolean a=false;
		rs = stm.executeUpdate();
		if(rs==1){
			return true;
			} else {
			return false;
			} 
		} catch (SQLException e) {
			return false;
		}
	}
	
	
	
	public static boolean change(String A,String B,String C) {//更改商品
		Connection conn=DBUtil.getConnectDb();//获取数据库的连接
		String sql = "UPDATE huojia set number=?,unit_price=? where commodity=?";
		int rs;
		PreparedStatement stm = null;
		try {
		stm = conn.prepareStatement(sql);
		stm.setString(1,B);
		stm.setString(2,C);
		stm.setString(3,A);
		
		boolean a=false;
		rs = stm.executeUpdate();
		if(rs==1){
			return true;
			} else {
			return false;
			} 
		} catch (SQLException e) {
			return false;
		}
	}
	
	
	
	
	public static boolean delete(String A) {//删除商品
		Connection conn=DBUtil.getConnectDb();
		String sql = "delete from huojia where commodity=?";
		int rs;
		PreparedStatement stm = null;
		try {
		stm = conn.prepareStatement(sql);
		stm.setString(1,A);
		
		boolean a=false;
		rs = stm.executeUpdate();
		if(rs==1){
			return true;
			} else {
			return false;
			} 
		} catch (SQLException e) {
			return false;
		}
	}
	
	
	
	
	public static ArrayList<huojiaBean> getLikeList() {//查询全部商品 存放多个商品信息的集合
		
		ArrayList<huojiaBean> tag_Array = new ArrayList<huojiaBean>();//建立一个数组集合
		Connection conn=DBUtil.getConnectDb();
		String sql = "select *from huojia";
		PreparedStatement stm = null;
		ResultSet rs = null;
		
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while(rs.next()) {//读取数据库
				huojiaBean tag = new huojiaBean();//查询商品 存放单个商品信息
				tag.setCommodity(rs.getString("commodity"));
				tag.setNumber(rs.getString("number"));
				tag.setUnit_price(rs.getString("unit_price"));
				tag_Array.add(tag);
			}
			rs.close();
			stm.close();
			
			
			} catch (SQLException e) {
				e.printStackTrace();
			}
		return tag_Array;
	}
	
	
	
	
	public static ArrayList<huojiaBean> getLikeList(String commodity) {//查询单个商品
			
			ArrayList<huojiaBean> tag_Array = new ArrayList<huojiaBean>();//建立一个数组集合
			Connection conn=DBUtil.getConnectDb();
			String sql = "select *from huojia where commodity=?";
			PreparedStatement stm = null;
			ResultSet rs = null;
			
			try {
				stm = conn.prepareStatement(sql);
				stm.setString(1,commodity);
				rs = stm.executeQuery();
				while(rs.next()) {//读取数据库
					huojiaBean tag = new huojiaBean();//查询商品 存放单个商品信息
					tag.setCommodity(rs.getString("commodity"));
					tag.setNumber(rs.getString("number"));
					tag.setUnit_price(rs.getString("unit_price"));
					tag_Array.add(tag);
				}
				rs.close();
				stm.close();
				
				
				} catch (SQLException e) {
					e.printStackTrace();
				}
			return tag_Array;
		}
	
	
	
	
}
